package com.part2.dao.function.Impl;

import java.util.ArrayList;
import java.util.List;

import com.part2.action.common.validator.Validator;
import com.part2.beans.News;
import com.part2.beans.Products;
import com.part2.common.query.Page;
import com.part2.dao.common.BaseDaoImpl;
import com.part2.dao.function.ProductsDao;

public class ProductsDaoImpl extends BaseDaoImpl<Products, String> implements
		ProductsDao {

	public Products findProductsByPrimeKey(String id) {
		// TODO Auto-generated method stub
		Products pro = null;
		pro = (Products) getSession().get(Products.class, id);
		return pro;
	}

	public Page<Products> findProducts(Page<Products> page, String bId) {
		// TODO Auto-generated method stub
		if(Validator.isNotNull(bId)){
		StringBuffer hql = new StringBuffer(" where e.bclassId = ?  ");
		if (page.getTotalCount() == -1)
			page.setTotalCount(
					super.findTotalCount(Products.class,hql.toString(),bId));		
			if(page.getTotalCount()!=0)
				return super.findByPage(page, " from Products e" + hql + "order by e.createDate asc",bId);
		else return null;
		}else {
			if (page.getTotalCount() == -1)
				page.setTotalCount(
						super.findTotalCount(Products.class));		
				if(page.getTotalCount()!=0)
					return super.findByPage(page, " from Products e order by e.createDate asc");
			else return null;
		}
	}

	public List<Products> findProductsByBigClass(String id) {
		// TODO Auto-generated method stub
		List<Products> p = new ArrayList<Products>();
		String hql = "from Products p where p.bclassId = ?";
		List<Object> tmp = find(hql, id);
		if (tmp != null && tmp.size() > 0) {
			for (Object o : tmp) {
				Products or = (Products) o;
				p.add(or);
			}
		}
		return p;
	}

	public Page<Products> findProductsBySearch(Page<Products> page, String key) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer(" where e.productName like ?  or e.productNameEn like ?   ");
//		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>type:"+type);
		if (page.getTotalCount() == -1)
			page.setTotalCount(
					super.findTotalCount(Products.class,hql.toString(),"%"+key+"%","%"+key+"%"));		
			if(page.getTotalCount()!=0)
				return super.findByPage(page, " from Products e " + hql + " order by e.createDate desc","%"+key+"%","%"+key+"%");
		else return null;
	}
	
}
